//
//  TTNetworkHelper.m
//  TTBaseProject
//
//  Created by shny588 on 16/7/19.
//  Copyright © 2016年 tt. All rights reserved.
//

#import "TTNetworkHelper.h"
#import "AFNetworking.h"
#import "MBProgressHUD.h"

@interface TTNetworkHelper ()

@property (nonatomic, strong) MBProgressHUD     *hud;

@end

@implementation TTNetworkHelper

//监测网络连接
+ (BOOL)isInternetReachable{
    return [[TTNetworkInfo shared] connectedToNetwork];
}

//网络请求时展示加载标记
- (void)showHUDAtView:(UIView*)view message:(NSString*)message{
    UIView *hudSuperView=view;
    if (!hudSuperView) {
        hudSuperView= [[UIApplication sharedApplication].delegate window];
    }
    _hud = [[MBProgressHUD alloc] initWithView:hudSuperView];
    _hud.removeFromSuperViewOnHide=YES;
    [hudSuperView addSubview:_hud];
    _hud.labelText = message;
    [_hud show:YES];
}

- (void)hideHUD{
    if (_hud) {
        [_hud hide:YES];
    }
}

//发送post请求
- (void)POST:(NSString *)URLString
  parameters:(id)parameters
     success:(void (^)(TTNetworkResult *result))success
     failure:(void (^)(NetworkErrorType errorType))failure
{
    if(![TTNetworkHelper isInternetReachable]){
        [self hideHUD];
        failure(NetworkErrorTypeNoNetwork);
        return;
    }
    NSAssert(_parseDelegate, @"error: QYNetworkHelper必须设置_parseDelegate");
    NSAssert(URLString, @"error: QYNetworkHelper URLString不能为空");
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.requestSerializer= [AFHTTPRequestSerializer serializer];
    manager.responseSerializer= [AFHTTPResponseSerializer serializer];
    [manager POST:URLString parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [self hideHUD];
        TTNetworkResult *result=[_parseDelegate convertResultData:operation.responseData  resultType:NetworkResultTypeNetwork];
        success(result);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self hideHUD];
        failure(NetworkErrorType404);
    }];
}

//发送get请求
- (void)GET:(NSString *)URLString
 parameters:(id)parameters
    success:(void (^)(TTNetworkResult *result))success
    failure:(void (^)(NetworkErrorType errorType))failure
{
    if(![TTNetworkHelper isInternetReachable]){
        [self hideHUD];
        failure(NetworkErrorTypeNoNetwork);
        return;
    }
    NSAssert(_parseDelegate, @"error: QYNetworkHelper必须设置_parseDelegate");
    NSAssert(URLString, @"error: QYNetworkHelper URLString不能为空");
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.requestSerializer= [AFHTTPRequestSerializer serializer];
    manager.responseSerializer= [AFHTTPResponseSerializer serializer];
    [manager GET:URLString parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [self hideHUD];
        TTNetworkResult *result=[_parseDelegate convertResultData:operation.responseData  resultType:NetworkResultTypeNetwork];
        success(result);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self hideHUD];
        failure(NetworkErrorType404);
    }];
}

//上传文件
- (void)upload:(NSString *)URLString
      fileData:(NSData*)fileData
      fileName:(NSString*)fileName
      mimeType:(NSString*)mimeType
    parameters:(id)parameters
       success:(void (^)(TTNetworkResult *result))success
       failure:(void (^)(NetworkErrorType errorType))failure
{
    if(![TTNetworkHelper isInternetReachable]){
        [self hideHUD];
        failure(NetworkErrorTypeNoNetwork);
        return;
    }
    NSAssert(_parseDelegate, @"error: QYNetworkHelper必须设置_parseDelegate");
    NSAssert(URLString, @"error: QYNetworkHelper URLString不能为空");
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.requestSerializer= [AFHTTPRequestSerializer serializer];
    manager.responseSerializer= [AFHTTPResponseSerializer serializer];
    [manager POST:URLString parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileData:fileData name:fileName fileName:[NSString stringWithFormat:@"%@.png",fileName] mimeType:mimeType];
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [self hideHUD];
        TTNetworkResult *result=[_parseDelegate convertResultData:operation.responseData  resultType:NetworkResultTypeNetwork];
        success(result);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self hideHUD];
        failure(NetworkErrorType404);
    }];
}

//上传图片，带进度进度
- (void)upload:(NSString *)URLString
      fileData:(NSData*)fileData
      fileName:(NSString*)fileName
      mimeType:(NSString*)mimeType
    parameters:(id)parameters
       success:(void (^)(TTNetworkResult *result))success
       failure:(void (^)(NetworkErrorType errorType))failure
      proGress:(void (^)(float progress))progress
{
    if(![TTNetworkHelper isInternetReachable]){
        [self hideHUD];
        failure(NetworkErrorTypeNoNetwork);
        return;
    }
    NSAssert(_parseDelegate, @"error: QYNetworkHelper必须设置_parseDelegate");
    NSAssert(URLString, @"error: QYNetworkHelper URLString不能为空");
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.requestSerializer= [AFHTTPRequestSerializer serializer];
    manager.responseSerializer= [AFHTTPResponseSerializer serializer];
    AFHTTPRequestOperation *operation=  [manager POST:URLString parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileData:fileData name:fileName fileName:[NSString stringWithFormat:@"%@.png",fileName] mimeType:mimeType];
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [self hideHUD];
        TTNetworkResult *result=[_parseDelegate convertResultData:operation.responseData  resultType:NetworkResultTypeNetwork];
        success(result);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self hideHUD];
        failure(NetworkErrorType404);
    }];
    [operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
        progress(totalBytesWritten*1.0/totalBytesExpectedToWrite);
    }];
}
@end
